package priv.lhy.log.config;

import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import priv.lhy.log.core.ServiceLogAdvice;
import priv.lhy.log.core.WebLogAdvice;

/**
 * @author : lihy
 * @date : 2021/8/5 10:02
 */
@Configuration
@Import({InterceptorConfig.class, LogProperties.class})
public class LogConfig {

    /*@Value("${aop.log.pointcut.web}")
    private String webPointcut;

    @Value("${aop.log.pointcut.service}")
    private String servicePointcut;*/

    @Bean
    public LogProperties getProperties(){
        return new LogProperties();
    }


    @ConditionalOnProperty(prefix = "aop.log", name = "webEnable", havingValue = "true")
    @Bean("webPointAdvisor")
    public AspectJExpressionPointcutAdvisor webPointAdvisor() {
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression(getProperties().getWebPointcut());
        advisor.setAdvice(new WebLogAdvice());
        return advisor;
    }

    @ConditionalOnProperty(prefix = "aop.log", name = "serviceEnable", havingValue = "true")
    @Bean("servicePointAdvisor")
    public AspectJExpressionPointcutAdvisor servicePointAdvisor() {
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression(getProperties().getServicePointcut());
        advisor.setAdvice(new ServiceLogAdvice());
        return advisor;
    }
}
